import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

let children = [
  {
    path: 'home',
    meta: {
      title: ''
    },
    component: () => import('../views/home/home.vue')
  },
  {
    path: 'menu',
    meta: {
      title: '菜单管理'
    },
    component: () => import('../views/menu/menu.vue')
  },
  {
    path: 'roles',
    meta: {
      title: '角色管理'
    },
    component: () => import('../views/roles/roles.vue')
  },
  ,
  {
    path: 'manger',
    meta: {
      title: '管理员管理'
    },
    component: () => import('../views/manger/manger.vue')
  },
  {
    path: '/category',
    meta: {
      title: '商品分类'
    },
    component: () => import('../views/category/category.vue')
  },
  {
    path: '/specs',
    meta: {
      title: '商品规格'
    },
    component: () => import('../views/specs//specs.vue')
  },
  {
    path: '/goods',
    meta: {
      title: '商品管理'
    },
    component: () => import('../views/goods/goods.vue')
  },
  {
    path: '/member',
    meta: {
      title: '会员管理'
    },
    component: () => import('../views/member/member.vue')
  },
  {
    path: '/banner',
    meta: {
      title: '轮播图管理'
    },
    component: () => import('../views/banner/banner.vue')
  },
  {
    path: '/seckill',
    meta: {
      title: '限时秒杀'
    },
    component: () => import('../views/seck/seck.vue')
  },
  {
    path: '',
    redirect: '/home'
  }
]
const routes = [
  {
    path: '/',
    component: () => import('../views/index.vue'),
    children,
  },
  {
    path: '/login',
    component: () => import('../views/login.vue')
  },

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path == '/login') {  //访问的是后面登录界面
    next();
    return;
  }
  //获取已登录的用户身份信息：
  let userObj = localStorage['userManger'] ? JSON.parse(localStorage['userManger']) : '';
  // console.log(userObj,777777);
  if (userObj == '') {  //未登录
    next('/login');
  } else {  //已登录
    //定义用户可以访问的路径：
    let menuArr = [...userObj.menus_url, '/', '/login','/home'];
    //判断用户要访问的目标地址是否允许：
    if (menuArr.indexOf(to.path) == -1) {  //不允许
      next('/login');
    } else {  //允许
      next();
    }
  }
})

export default router
